package com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.stat;

import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2025/2/25
 */
@Data
public class PerformanceChartPerDateDetail {
    /**
     * 日期
     */
    private LocalDate date;

    /**
     * 每个运维人员当天有效次数
     */
    private List<PerformancePieChartData<Integer>> validCount;

    /**
     * 每个运维人员当天绩效总和
     */
    private List<PerformancePieChartData<BigDecimal>> performanceSum;


    public static List<PerformanceChartPerDateDetail> fromDo(Map<Integer, String> accountUserMap, List<PerformanceChartDo> chartDos, LocalDate startDate, LocalDate endDate) {
        if (chartDos == null) {
            chartDos = Collections.emptyList();
        }
        // 根据日期和运维人员ID为key分组
        Map<String, PerformanceChartDo> chartDoMap = chartDos.stream().collect(Collectors.toMap(v -> v.getDate() + "_" + v.getAccountUserId(), Function.identity(), (v1, v2) -> v1));
        List<PerformanceChartPerDateDetail> details = new ArrayList<>();
        // 组装数据 每个日期 每个运维人员 都一个数据
        for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
            LocalDate finalDate = date;
            PerformanceChartPerDateDetail detail = new PerformanceChartPerDateDetail();
            detail.setDate(date);
            List<PerformancePieChartData<Integer>> validCount = new ArrayList<>(accountUserMap.size());
            List<PerformancePieChartData<BigDecimal>> performanceSum = new ArrayList<>(accountUserMap.size());
            accountUserMap.forEach((k, v) -> {
                // 组装PerformancePieChartData数据
                PerformancePieChartData<Integer> validCountData = new PerformancePieChartData<>();
                validCountData.setAccountUserId(k);
                validCountData.setAccountUserName(v);
                PerformancePieChartData<BigDecimal> performanceSumData = new PerformancePieChartData<>();
                performanceSumData.setAccountUserId(k);
                performanceSumData.setAccountUserName(v);
                // 根据当前日期和运维人员ID获取对应的PerformanceChartDo对象
                PerformanceChartDo chartDo = chartDoMap.get(finalDate + "_" + k);
                if (chartDo == null) {
                    validCountData.setData(0);
                    performanceSumData.setData(BigDecimal.ZERO);
                } else {
                    validCountData.setData(chartDo.getValidCount());
                    performanceSumData.setData(chartDo.getPerformanceSum());
                }
                validCount.add(validCountData);
                performanceSum.add(performanceSumData);
            });
            detail.setValidCount(validCount);
            detail.setPerformanceSum(performanceSum);
            details.add(detail);
        }
        return details;
    }
}
